/**
 * Copyright (c) 2009  Collaborative Development Group, C.S. Dept., University of Bari
 *
 * All rights reserved. This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License v1.0  which accompanies this distribution, and is 
 * available at http://www.eclipse.org/legal/epl-v10.html
 */
package it.uniba.di.cdg.jtalk.ui.login;

import static it.uniba.di.cdg.jtalk.ui.login.ILoginPresentationModel.AUTOMATIC_LOGIN_PROPERTY;
import static it.uniba.di.cdg.jtalk.ui.login.ILoginPresentationModel.PASSWORD_PROPERTY;
import static it.uniba.di.cdg.jtalk.ui.login.ILoginPresentationModel.REMEMBER_PASSWORD_PROPERTY;
import static it.uniba.di.cdg.jtalk.ui.login.ILoginPresentationModel.USER_ID_PROPERTY;
import static it.uniba.di.cdg.penelope.presenterfirst.PropertyMatcher.isProperty;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.runners.MockitoJUnitRunner;

/**
 * Unit tests for {@link LoginPresentationModel} implementation.
 */
@RunWith( MockitoJUnitRunner.class ) 
public class LoginPresentationModelTest {
	
	private LoginPresentationModel presentationModel;
	private PropertyChangeListener listener;

	@Before
	public void setUp() {
		this.presentationModel = new LoginPresentationModel();
		this.listener = mock( PropertyChangeListener.class );
		
		presentationModel.addPropertyChangeListener( listener );
	}
	
	@Test
	public void shouldNotifyUserIdChange() {
		presentationModel.setUserId( "new" );
	
		assertPropertyChangeListenerNotified( USER_ID_PROPERTY, "", "new" );
	}

	@Test
	public void shouldNotifyPasswordChange() {
		presentationModel.setPassword( "newpassword" );
	
		assertPropertyChangeListenerNotified( PASSWORD_PROPERTY, "", "newpassword" );
	}

	@Test
	public void shouldNotifyAutomaticLoginChange() {
		presentationModel.setAutomaticLogin( true );
	
		assertPropertyChangeListenerNotified( AUTOMATIC_LOGIN_PROPERTY, false, true );
	}

	@Test
	public void shouldNotifyRememberPasswordChange() {
		presentationModel.setRememberPassword( true );
	
		assertPropertyChangeListenerNotified( REMEMBER_PASSWORD_PROPERTY, false, true );
	}
	
	private void assertPropertyChangeListenerNotified( String propertyName, Object oldValue, Object newValue ) {
		ArgumentCaptor<PropertyChangeEvent> argument = ArgumentCaptor.forClass( PropertyChangeEvent.class );
		
		verify( listener ).propertyChange( argument.capture() );
		assertThat( argument.getValue(), isProperty( propertyName, oldValue, newValue ) );
	}
}
